package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.common.utils.math.RandomUtils;
import com.sghd.fight.module.game.config.ElementConfig;
import com.sghd.fight.module.game.config.ElementPk;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.room.Monster;
import com.sghd.fight.module.game.service.room.Player;
import com.sghd.fight.module.game.service.skill.ConfigService;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.fight.module.game.service.skill.TargetState;
import com.sghd.pojo.fight.game.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class AttackEffect implements Effect {
    @Autowired
    private ConfigService configService;

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        Monster monster = (Monster) owner;
        int attack = monster.getMonsterConfig().getAttack();
        int val = (int) (attack * getBet(owner, target));
        double dodge = 0.0;
        if (target instanceof Player){
            Player player = (Player) target;
            Map<ChessType,Integer> map =  player.getElement2Level();
            for (ChessType chessType : ChessType.values()) {
                if (chessType == ChessType.CT_LIGHTNING || map.get(chessType)==null|| map.get(chessType) == 0) {
                    continue;
                }
                int level = map.get(chessType);
                for (; level > 0; level--) {
                    ElementPk pk = ElementPk.valueOf(chessType, level);
                    ElementConfig attrConfig = configService.getAttr(pk);
                    dodge += attrConfig.getAttr(room.getType()).getExtraDodge();
                }
            }
        }
        if (RandomUtils.isHit(dodge)){
            val = 0;
            logger.debug("闪避成功");
            if (!targetReport.hasStatus(TargetState.DODGE)){
                targetReport.addStatus(TargetState.DODGE);
            }
        }
        val = fixHp(owner, target, val, room, config);
        context.addValue(target, UnitValue.HP, -val);
        targetReport.addValue(ValueItem.valueOf(UnitValue.HP, -val));
    }

    @Override
    public EffectType getType() {
        return EffectType.SK_ATTACK;
    }
}
